
#ifndef __RSA_PRIVATE_KEY_H
#define __RSA_PRIVATE_KEY_H

#include "keys.h"
#include <openssl/rsa.h>
#include "../iface/SSLInterface.h"

class RSAPrivateKey : public PrivateKey, public DecryptionKey, public SigningKey
{
   public:
      RSAPrivateKey();
      RSAPrivateKey(const char *name);
      // Construct a duplicate of the private key
      RSAPrivateKey(const RSAPrivateKey &other);
      // Construct from PEM format
      RSAPrivateKey(const char *pemtext, const char *name);
      // Construct from encrypted PEM format
      RSAPrivateKey(const char *pemtext, const char *password, const char *name);
      // Construct from OpenSSL format
      RSAPrivateKey(RSA *prvkey, const char *name);
      // Destroy RSA private key
      ~RSAPrivateKey();
      // Get context
      int GetCryptlibContext(CRYPT_CONTEXT *keyContext);
      // Get Decryption context
      int GetCryptlibDecryptionContext(CRYPT_CONTEXT *keyContext);
      // Get Signing context
      int GetCryptlibSigningContext(CRYPT_CONTEXT *keyContext);
      // Get key components
      int GetCryptlibKeyComponents(CRYPT_PKCINFO_RSA *rsaKey);
      // Get new OpenSSL RSA object
      int GetSSLKeyComponents(RSA **prvkey);
      // Get PEM data
      int GetPEMKeyData(char **pemtext);
      // Get encrypted PEM data
      int GetPEMKeyData(char **pemtext, const char *password);
      // Compare key to key
      int CompareKeyBelongs(PublicKey *pubkey);
      // Get key type name
      int GetKeyType() { return KEY_TYPE_PRIVATE_RSA; }
      // To import from text - Base64 RAW or PEM
      int ImportKeyFromASCII(const char *text, const char *name);
      // To export to text - Base64 RAW or PEM
      int ExportKeyToASCII(char **text);
      // Import from text with password
      int ImportKeyFromASCIIWithPassword(const char *text, const char *password, const char *name);
      // Export to text with password
      int ExportKeyToASCIIWithPassword(char **text, const char *password);
   private:
      // Copy SSL type public keys
      static int CopyRSASSLPrivateKey(RSA *dst, RSA *src);
      // Import private key (with optional password if it's encrypted)
      int ImportPrivateKeyWithOptionalPassword(const char *pemkeydata, const char *password);
      RSA *key;
};

#endif
